// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.iso.weather;

import org.lwjgl.opengl.ARBShaderObjects;
import zombie.characters.IsoPlayer;
import zombie.core.Core;
import zombie.core.PerformanceSettings;
import zombie.core.opengl.RenderSettings;
import zombie.core.opengl.Shader;
import zombie.core.opengl.ShaderProgram;
import zombie.core.textures.TextureDraw;
import zombie.iso.IsoCamera;
import zombie.iso.PlayerCamera;
import zombie.iso.SearchMode;

public class WeatherShader extends Shader {
    public int timeOfDay = 0;
    private int PixelOffset;
    private int PixelSize;
    private int bloom;
    private int timer;
    private int BlurStrength;
    private int TextureSize;
    private int Zoom;
    private int Light;
    private int LightIntensity;
    private int NightValue;
    private int Exterior;
    private int NightVisionGoggles;
    private int DesaturationVal;
    private int FogMod;
    private int SearchModeID;
    private int ScreenInfo;
    private int ParamInfo;
    private int VarInfo;
    private int timerVal;
    private boolean bAlt = false;
    private static final int texdVarsSize = 22;
    private static float[][] floatArrs = new float[5][];

    public WeatherShader(String name) {
        super(name);
    }

    @Override
    public void startMainThread(TextureDraw texd, int playerIndex) {
        if (playerIndex >= 0 && playerIndex < 4) {
            RenderSettings.PlayerRenderSettings playerRenderSettings = RenderSettings.getInstance().getPlayerSettings(playerIndex);
            IsoPlayer isoPlayer = IsoPlayer.players[playerIndex];
            boolean _boolean = playerRenderSettings.isExterior();
            float _float = zombie.GameTime.instance.TimeOfDay / 12.0F - 1.0F;
            if (Math.abs(_float) > 0.8F && isoPlayer != null && isoPlayer.Traits.NightVision.isSet() && !isoPlayer.isWearingNightVisionGoggles()) {
                _float *= 0.8F;
            }

            int int0 = Core.getInstance().getOffscreenWidth(playerIndex);
            int int1 = Core.getInstance().getOffscreenHeight(playerIndex);
            if (texd.vars == null) {
                texd.vars = getFreeFloatArray();
                if (texd.vars == null) {
                    texd.vars = new float[22];
                }
            }

            texd.vars[0] = playerRenderSettings.getBlendColor().r;
            texd.vars[1] = playerRenderSettings.getBlendColor().g;
            texd.vars[2] = playerRenderSettings.getBlendColor().b;
            texd.vars[3] = playerRenderSettings.getBlendIntensity();
            texd.vars[4] = playerRenderSettings.getDesaturation();
            texd.vars[5] = playerRenderSettings.isApplyNightVisionGoggles() ? 1.0F : 0.0F;
            SearchMode.PlayerSearchMode playerSearchMode = SearchMode.getInstance().getSearchModeForPlayer(playerIndex);
            texd.vars[6] = playerSearchMode.getShaderBlur();
            texd.vars[7] = playerSearchMode.getShaderRadius();
            texd.vars[8] = (float)IsoCamera.getOffscreenLeft(playerIndex);
            texd.vars[9] = (float)IsoCamera.getOffscreenTop(playerIndex);
            PlayerCamera playerCamera = IsoCamera.cameras[playerIndex];
            texd.vars[10] = (float)IsoCamera.getOffscreenWidth(playerIndex);
            texd.vars[11] = (float)IsoCamera.getOffscreenHeight(playerIndex);
            texd.vars[12] = playerCamera.RightClickX;
            texd.vars[13] = playerCamera.RightClickY;
            texd.vars[14] = Core.getInstance().getZoom(playerIndex);
            texd.vars[15] = Core.TileScale == 2 ? 64.0F : 32.0F;
            texd.vars[16] = playerSearchMode.getShaderGradientWidth() * texd.vars[15] / 2.0F;
            texd.vars[17] = playerSearchMode.getShaderDesat();
            texd.vars[18] = playerSearchMode.isShaderEnabled() ? 1.0F : 0.0F;
            texd.vars[19] = playerSearchMode.getShaderDarkness();
            texd.flipped = playerRenderSettings.isExterior();
            texd.f1 = playerRenderSettings.getDarkness();
            texd.col0 = int0;
            texd.col1 = int1;
            texd.col2 = Core.getInstance().getOffscreenTrueWidth();
            texd.col3 = Core.getInstance().getOffscreenTrueHeight();
            texd.bSingleCol = Core.getInstance().getZoom(playerIndex) > 2.0F
                || (double)Core.getInstance().getZoom(playerIndex) < 2.0 && Core.getInstance().getZoom(playerIndex) >= 1.75F;
        }
    }

    @Override
    public void startRenderThread(TextureDraw texd) {
        float float0 = texd.f1;
        boolean _boolean = texd.flipped;
        int int0 = texd.col0;
        int int1 = texd.col1;
        int int2 = texd.col2;
        int int3 = texd.col3;
        float float1 = texd.bSingleCol ? 1.0F : 0.0F;
        ARBShaderObjects.glUniform1fARB(this.width, (float)int0);
        ARBShaderObjects.glUniform1fARB(this.height, (float)int1);
        ARBShaderObjects.glUniform3fARB(this.Light, texd.vars[0], texd.vars[1], texd.vars[2]);
        ARBShaderObjects.glUniform1fARB(this.LightIntensity, texd.vars[3]);
        ARBShaderObjects.glUniform1fARB(this.NightValue, float0);
        ARBShaderObjects.glUniform1fARB(this.DesaturationVal, texd.vars[4]);
        ARBShaderObjects.glUniform1fARB(this.NightVisionGoggles, texd.vars[5]);
        ARBShaderObjects.glUniform1fARB(this.Exterior, _boolean ? 1.0F : 0.0F);
        ARBShaderObjects.glUniform1fARB(this.timer, (float)(this.timerVal / 2));
        if (PerformanceSettings.getLockFPS() >= 60) {
            if (this.bAlt) {
                this.timerVal++;
            }

            this.bAlt = !this.bAlt;
        } else {
            this.timerVal += 2;
        }

        float float2 = 0.0F;
        float float3 = 0.0F;
        float float4 = 1.0F / (float)int0;
        float float5 = 1.0F / (float)int1;
        ARBShaderObjects.glUniform2fARB(this.TextureSize, (float)int2, (float)int3);
        ARBShaderObjects.glUniform1fARB(this.Zoom, float1);
        ARBShaderObjects.glUniform4fARB(this.SearchModeID, texd.vars[6], texd.vars[7], texd.vars[8], texd.vars[9]);
        ARBShaderObjects.glUniform4fARB(this.ScreenInfo, texd.vars[10], texd.vars[11], texd.vars[12], texd.vars[13]);
        ARBShaderObjects.glUniform4fARB(this.ParamInfo, texd.vars[14], texd.vars[15], texd.vars[16], texd.vars[17]);
        ARBShaderObjects.glUniform4fARB(this.VarInfo, texd.vars[18], texd.vars[19], texd.vars[20], texd.vars[21]);
    }

    @Override
    public void onCompileSuccess(ShaderProgram sender) {
        int _int = this.getID();
        this.timeOfDay = ARBShaderObjects.glGetUniformLocationARB(_int, "TimeOfDay");
        this.bloom = ARBShaderObjects.glGetUniformLocationARB(_int, "BloomVal");
        this.PixelOffset = ARBShaderObjects.glGetUniformLocationARB(_int, "PixelOffset");
        this.PixelSize = ARBShaderObjects.glGetUniformLocationARB(_int, "PixelSize");
        this.BlurStrength = ARBShaderObjects.glGetUniformLocationARB(_int, "BlurStrength");
        this.width = ARBShaderObjects.glGetUniformLocationARB(_int, "bgl_RenderedTextureWidth");
        this.height = ARBShaderObjects.glGetUniformLocationARB(_int, "bgl_RenderedTextureHeight");
        this.timer = ARBShaderObjects.glGetUniformLocationARB(_int, "timer");
        this.TextureSize = ARBShaderObjects.glGetUniformLocationARB(_int, "TextureSize");
        this.Zoom = ARBShaderObjects.glGetUniformLocationARB(_int, "Zoom");
        this.Light = ARBShaderObjects.glGetUniformLocationARB(_int, "Light");
        this.LightIntensity = ARBShaderObjects.glGetUniformLocationARB(_int, "LightIntensity");
        this.NightValue = ARBShaderObjects.glGetUniformLocationARB(_int, "NightValue");
        this.Exterior = ARBShaderObjects.glGetUniformLocationARB(_int, "Exterior");
        this.NightVisionGoggles = ARBShaderObjects.glGetUniformLocationARB(_int, "NightVisionGoggles");
        this.DesaturationVal = ARBShaderObjects.glGetUniformLocationARB(_int, "DesaturationVal");
        this.FogMod = ARBShaderObjects.glGetUniformLocationARB(_int, "FogMod");
        this.SearchModeID = ARBShaderObjects.glGetUniformLocationARB(_int, "SearchMode");
        this.ScreenInfo = ARBShaderObjects.glGetUniformLocationARB(_int, "ScreenInfo");
        this.ParamInfo = ARBShaderObjects.glGetUniformLocationARB(_int, "ParamInfo");
        this.VarInfo = ARBShaderObjects.glGetUniformLocationARB(_int, "VarInfo");
    }

    @Override
    public void postRender(TextureDraw texd) {
        if (texd.vars != null) {
            returnFloatArray(texd.vars);
            texd.vars = null;
        }
    }

    private static float[] getFreeFloatArray() {
        for (int _int = 0; _int < floatArrs.length; _int++) {
            if (floatArrs[_int] != null) {
                float[] _float = floatArrs[_int];
                floatArrs[_int] = null;
                return _float;
            }
        }

        return new float[22];
    }

    private static void returnFloatArray(float[] _float) {
        for (int _int = 0; _int < floatArrs.length; _int++) {
            if (floatArrs[_int] == null) {
                floatArrs[_int] = _float;
                break;
            }
        }
    }
}
